package net.stratusprogramming.school.Game.Framework.GUI;

import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;

import net.stratusprogramming.school.Game.Framework.Scene.Actor;
import net.stratusprogramming.school.Game.Framework.System.InstanceMgr;
import net.stratusprogramming.school.Game.Framework.System.Logger;
import net.stratusprogramming.school.Game.Framework.System.SaveMgr;

/**
 * A class for a menu item/button/whatever
 * 
 * @author Lienau
 * @since 5/16/12
 * @version 0.1
 */
public class TextButton extends Actor {

	/** Input from slick */
	private Input input;

	/** Sets the color of the font */
	private Color fontColor;

	/** Font to use to draw menu item with */
	private Font font;

	/** If the text should be aligned to the right */
	private boolean alignRight;

	/** Text of menu item */
	private String text;

	/**
	 * Creates a new menu item
	 * 
	 * @param font
	 *            font of menu item to use
	 * @param text
	 *            text to display
	 */
	public TextButton(Font font, String text) {
		this.font = font;
		setText(text);
		fontColor = Color.white;

		initBoundingBox();
		input = InstanceMgr.input;
	}

	/**
	 * Creates an empty menu item using a specific font
	 */
	public TextButton(Font font) {
		this(font, "");

	}

	/**
	 * Creates an empty menu Item.
	 * 
	 * <p>
	 * Note: Arial 20pt will be used as a default.
	 * </p>
	 */
	public TextButton() {
		this(Fonts.Arial20pt, "");
	}

	/**
	 * Checks if the menu item was clicked
	 * 
	 * @return true if clicked, false else
	 */
	public boolean isClicked() {
		// get mouse coords
		float xp = input.getMouseX();
		float yp = input.getMouseY();

		// adjust if right-align
		if (alignRight)
			xp = xp + width;

		// check if click was in bounds
		return boundingBox.contains(xp, yp);
	}

	@Override
	public void update(int delta) {
		updateBoundingBox();
	}

	@Override
	public void render(Graphics g) {
		if (!alignRight)
			font.drawString(x, y, text, fontColor);
		else
			font.drawString(x - width, y, text, fontColor);
	}

	@Override
	public void save(SaveMgr saveMgr) {
		// TODO Auto-generated method stub

	}

	@Override
	public void load(SaveMgr saveMgr) {
		// TODO Auto-generated method stub

	}

	/**
	 * @return the fontColor
	 */
	public Color getFontColor() {
		return fontColor;
	}

	/**
	 * @param fontColor
	 *            the fontColor to set
	 */
	public void setFontColor(Color fontColor) {
		this.fontColor = fontColor;
	}

	/**
	 * @return the font
	 */
	public Font getFont() {
		return font;
	}

	/**
	 * @param font
	 *            the font to set
	 */
	public void setFont(Font font) {
		this.font = font;
		width = font.getWidth(text);
		height = font.getHeight(text);
	}

	/**
	 * @return the alignRight
	 */
	public boolean isAlignRight() {
		return alignRight;
	}

	/**
	 * @param alignRight
	 *            the alignRight to set
	 */
	public void setAlignRight(boolean alignRight) {
		this.alignRight = alignRight;
	}

	/**
	 * @return the text
	 */
	public String getText() {
		return text;
	}

	/**
	 * @param text
	 *            the text to set
	 */
	public void setText(String text) {
		this.text = text;
		width = font.getWidth(text);
		height = font.getHeight(text);
	}
}
